import Jkdk.SendEmail;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;

import java.security.GeneralSecurityException;
import java.util.concurrent.TimeUnit;

/**
 * @author yyq
 * @create 2021-08-30 14:56
 */
public class Test {
    public static void main(String[] args) throws InterruptedException, GeneralSecurityException {

        //忽略不安全连接
        ChromeOptions opts = new ChromeOptions();
        //不启动浏览器
        opts.addArguments("--headless");
        opts.addArguments("--disable-gpu");

        //ignore-certificate-errors
        opts.addArguments("ignore-certificate-errors");
        opts.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\yyq\\Downloads\\chromedriver.exe");
        WebDriver webDriver = new ChromeDriver(opts);


        webDriver.manage().window().maximize();
        webDriver.manage().deleteAllCookies();
        // 与浏览器同步非常重要，必须等待浏览器加载完毕
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        webDriver.get("https://jksb.v.zzu.edu.cn/vls6sss/zzujksb.dll/first0");
        //Thread.sleep(1000);
        webDriver.findElement(new By.ByName("uid")).sendKeys("20172430735");
        webDriver.findElement(new By.ByName("upw")).sendKeys("11282151");
        webDriver.findElement(new By.ByName("smbtn")).click();

        //查看是否打卡
        //确定页面加载完成

        //Thread.sleep(1000);
        //坑一 切换iframe
        webDriver.switchTo().frame(webDriver.findElement(By.id("zzj_top_6s")));
        String text = webDriver.findElement(new By.ByXPath("/html/body/form/div/div[7]/span")).getText();

        if (text == "今日您已经填报过了")
            System.out.println("今日已打卡");
        else {
            webDriver.findElement(new By.ByXPath("//*[@id=\"bak_0\"]/div[13]/div[5]/div[4]/span")).click();


            ((JavascriptExecutor) webDriver).executeScript(
                         "document.getElementsByName(\"myvs_1\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_2\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_3\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_4\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_5\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_6\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_7\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_8\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_9\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_10\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_11\")[1].checked=true;" +
                            "document.getElementsByName(\"myvs_12\")[1].checked=true;");

            ((JavascriptExecutor) webDriver).executeScript(
                    "document.getElementsByName(\"myvs_13\")[0].options[0].selected=true;"+
                            "document.getElementsByName(\"myvs_24\")[1].checked=true;"+
                            "document.getElementsByTagName(\"span\")[3].click()"
            );
                            //地址先不变动
            // while(document.getElementsByName("memo22")[0].value=="成功获取"){document.getElementsByTagName("span")[3].click()  }

           // Thread.sleep(1000);
            WebElement div = webDriver.findElement(By.xpath("/html/body/form/div[1]/div[2]/div[2]/div[2]/div[2]"));
            System.out.println(div.getText());
            SendEmail.send("463757390@qq.com",div.getText());
            webDriver.close();
            webDriver.quit();

        }
    }
}
